// pages/combine/combine.js
const app = getApp()
Page({
    data: {},

    onLoad: function (options) {
       console.log(app.globalData.bgPic)
        wx.getImageInfo({
            src: app.globalData.bgPic,
            success: res => {
                this.bgPic = res.path
                this.draw()
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    draw () {
        let scale = app.globalData.scale
        let rotate = app.globalData.rotate
        let hat_center_x = app.globalData.hat_center_x
        let hat_center_y = app.globalData.hat_center_y
        let currentHatId = app.globalData.currentHatId
        const pc = wx.createCanvasContext("myCanvas")
        const hat_size = 100 * scale

        pc.clearRect(0, 0, 300, 300)
        pc.drawImage(this.bgPic, 0, 0, 300, 300)
        pc.translate(hat_center_x, hat_center_y)
        pc.rotate((rotate * Math.PI) / 180)
        pc.drawImage(
            "../../image/" + currentHatId + ".png",
            -hat_size / 2,
            -hat_size / 2,
            hat_size,
            hat_size
        )
        pc.draw()
    },
    savePic () {
        wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            height: 300,
            width: 300,
            canvasId: "myCanvas",
            success: result => {
                app.globalData.successPic = result.tempFilePath
                wx.saveImageToPhotosAlbum({
                    filePath: result.tempFilePath,
                    success: res => {
                        this.uploadImage(result)
                        wx.navigateTo({
                            url: "../share/share",
                            success: function (res) {
                            },
                            fail: function (res) {
                            },
                            complete: function (res) {
                            }
                        })
                        // console.log("success:" + res);
                    },
                    fail (e) {
                        console.log("err:" , e)
                    }
                })
            }
        })
    },
    uploadImage (res) {
        wx.uploadFile({
            url: 'https://ytfunclub.cn/api/v1/file/upload',
            name: 'myFiles',
            filePath: res.tempFilePath,
            header: {
                'Content-type': 'form-data'
            },
            success (res) {
                console.log(res)
            },
            fail (res) {
                console.log(res)
            }
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let successPic = app.globalData.successPic
            ? app.globalData.successPic
            : "https://mp.weixin.qq.com/wxopen/basicprofile?action=get_headimg&token=781774970&t=1636953638691"
        return {
            title: "一起来为头像带上口罩吧！",
            imageUrl: successPic,
            path: "/pages/index/index",
            success: function (res) {
            }
        }
    }
})
